package com.thfw.test.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.thfw.wmd.R;
import com.thfw.test.model.bean.CameraBean;
import com.thfw.test.model.bean.GatewayBean;
import com.thfw.test.model.bean.LampListBean;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created BY Cuishikun
 * Created ON 2021/11/17
 * Package Name com.thfw.test.view.adapter
 **/

public class ExpandableListviewAdapter extends BaseExpandableListAdapter {
    //Model：定义的数据
    private String[] groups;
    //注意，字符数组不要写成{{"A1,A2,A3,A4"}, {"B1,B2,B3,B4，B5"}, {"C1,C2,C3,C4"}}
    private HashMap<String, ArrayList<Object>> childs;
    private Context context;

    public ExpandableListviewAdapter(Context context, String[] groups, HashMap<String, ArrayList<Object>> childs){
        this.context=context;
        this.groups=groups;
        this.childs=childs;
    }

    @Override
    public int getGroupCount() {
        return groups.length;
    }

    @Override
    public int getChildrenCount(int i) {
        return childs.get(groups[i]).size();
    }

    @Override
    public Object getGroup(int i) {
        return groups[i];
    }

    @Override
    public Object getChild(int i, int i1) {
        return childs.get(groups[i]).get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    //分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
    public boolean hasStableIds() {
        return true;
    }

    @Override
/**
 *
 * 获取显示指定组的视图对象
 *
 * @param groupPosition 组位置
 * @param isExpanded 该组是展开状态还是伸缩状态，true=展开
 * @param convertView 重用已有的视图对象
 * @param parent 返回的视图对象始终依附于的视图组
 */
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_parent,parent,false);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.parent_textview_id = convertView.findViewById(R.id.parent_textview_id);
            groupViewHolder.parent_image = convertView.findViewById(R.id.parent_image);
            convertView.setTag(groupViewHolder);
        }else {
            groupViewHolder = (GroupViewHolder)convertView.getTag();
        }
        groupViewHolder.parent_textview_id.setText(groups[groupPosition]);
        //如果是展开状态，
        if (isExpanded){
            groupViewHolder.parent_image.setImageDrawable(ContextCompat.getDrawable(context,R.mipmap.icon_up));
        }else{
            groupViewHolder.parent_image.setImageDrawable(ContextCompat.getDrawable(context,R.mipmap.icon_down));
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView==null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_child,parent,false);
            childViewHolder = new ChildViewHolder();
            childViewHolder.chidren_item = (TextView)convertView.findViewById(R.id.chidren_item);
            childViewHolder.ll_lamp = convertView.findViewById(R.id.ll_lamp);
            childViewHolder.ll_camera = convertView.findViewById(R.id.ll_camera);
            childViewHolder.ll_gateway = convertView.findViewById(R.id.ll_gateway);

            childViewHolder.tv_state_lamp = convertView.findViewById(R.id.tv_state_lamp);

            childViewHolder.tv_ip_camera = convertView.findViewById(R.id.tv_ip_camera);
            childViewHolder.tv_id_camera = convertView.findViewById(R.id.tv_id_camera);

            childViewHolder.tv_num_gateway = convertView.findViewById(R.id.tv_num_gateway);
            childViewHolder.tv_state_gateway = convertView.findViewById(R.id.tv_state_gateway);
            convertView.setTag(childViewHolder);

        }else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        Object obj = childs.get(groups[groupPosition]).get(childPosition);
        if(childs.get(groups[groupPosition]).get(childPosition) instanceof LampListBean){
            childViewHolder.ll_lamp.setVisibility(View.VISIBLE);
            childViewHolder.ll_camera.setVisibility(View.GONE);
            childViewHolder.ll_gateway.setVisibility(View.GONE);
            childViewHolder.tv_state_lamp.setText((((LampListBean)obj).getLampType()).equals("0")?"设备状态：离线":"设备状态：在线");
            childViewHolder.chidren_item.setText(((LampListBean)obj).getLampId());
        }else if(childs.get(groups[groupPosition]).get(childPosition) instanceof GatewayBean){
            childViewHolder.ll_lamp.setVisibility(View.GONE);
            childViewHolder.ll_camera.setVisibility(View.GONE);
            childViewHolder.ll_gateway.setVisibility(View.VISIBLE);

            childViewHolder.tv_num_gateway.setText("当前设备连接数："+((GatewayBean)obj).getConnectNum());
            childViewHolder.tv_state_gateway.setText((((GatewayBean)obj).getGatewayType()).equals("0")?"设备状态：离线":"设备状态：在线");
        }else if(childs.get(groups[groupPosition]).get(childPosition) instanceof CameraBean){

            childViewHolder.ll_lamp.setVisibility(View.GONE);
            childViewHolder.ll_camera.setVisibility(View.VISIBLE);
            childViewHolder.ll_gateway.setVisibility(View.GONE);

            childViewHolder.tv_id_camera.setText("设备ID："+((CameraBean)obj).getCameraId());
            childViewHolder.tv_ip_camera.setText("设备IP："+((CameraBean)obj).getCameraIp());
        }

        return convertView;
    }

    //指定位置上的子元素是否可选中
    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }
    static class GroupViewHolder {
        TextView parent_textview_id;
        ImageView parent_image;
    }

    static class ChildViewHolder {
        TextView chidren_item;
        LinearLayout ll_lamp;
        LinearLayout ll_camera;
        LinearLayout ll_gateway;


        TextView tv_state_lamp;


        TextView tv_ip_camera;
        TextView tv_id_camera;

        TextView tv_num_gateway;
        TextView tv_state_gateway;
    }
}
